﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ECCSystem.EntityClass;
using ECCSystem.BizRule;
using ECCSystem.CheckClass;
using ECC_EXT_DAO.Dao;
using ECC_EXT_Entity;
using ECC_EXT_HttpLib;
using log4net;

namespace ECCSystem
{
	public partial class frmEntryInput : Form
	{

		TypeTemp objTypeTemp;
		ClsEnumInputMode objClsEnumInputMode;
		ClsEnumInputMode objClsEnumInputModeTmp;
		float fltOperationTime = 0.0f;
		DateTime dtOperationStart = new DateTime();
		int intOT = 0;
		string strOTKeyID = "";
		TrackTaskBillEntity objTrackTaskBillEntity;
		string strMailCheckID = "";
		frmTemplateImportAndInput objfrmTemplateImportAndInput = null;

		string strKeyNam = "";
		public frmEntryInput()
		{
			InitializeComponent();
		}

		public frmEntryInput(TypeTemp objFlg,TrackTaskBillEntity objEntity, ClsEnumInputMode objInputFlg, DateTime dtTmp)
		{
			objClsEnumInputMode = objInputFlg;
			objTypeTemp = objFlg;
			objTrackTaskBillEntity = objEntity;
			InitializeComponent();
			dtOperationStart = dtTmp;
			objClsEnumInputModeTmp = objClsEnumInputMode;
		}

		private void frmEntryInput_Load(object sender, EventArgs e)
		{
			objfrmTemplateImportAndInput = new frmTemplateImportAndInput(1);
			objfrmTemplateImportAndInput.Show();
			switch (objClsEnumInputMode)
			{
				case ClsEnumInputMode.Enum_TypeStrat:
					break;
				case ClsEnumInputMode.Enum_TypeSendAgain:
					btnSend.Text = "再転送";
					break;
				case ClsEnumInputMode.Enum_TypeEnd:
					break;
				case ClsEnumInputMode.Enum_TypeDepent:
					break;
				case ClsEnumInputMode.Enum_TypeMailCheck:
					break;
				default:
					break;
			}

			BindData();

            MailInfoListenThread =
                new Thread(new ThreadStart(GetMailInfo));//Mail Adapter
            MailInfoListenThread.Start();
			if (objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			{
				if (objTypeTemp == TypeTemp.Enum_TypeTempAdd)
				{
					btnSend.Enabled = false;
					btnDepend.Enabled = false;
					btnSave.Enabled = false;
					//ckbAppoint.Enabled = false;
					//cmbMailCategory.Enabled = false;
					//cmbKeyName.Enabled = false;
				}
				else if (objTypeTemp == TypeTemp.Enum_TypeTempUpdate)
				{
					btnStart.Enabled = false;
					ckbMailCheck.Enabled = false;
					ckbAppoint.Enabled = false;
					ckbSVHelp.Enabled = false;
					//ckbMailCheck.Enabled = false;
					cmbMailCategory.Enabled = false;
					cmbKeyName.Enabled = false;
					cmbMailCategory.SelectedValue = objTrackTaskBillEntity.CategoryScore_KeyID;
					cmbKeyName.SelectedValue = objTrackTaskBillEntity.CategoryContent_KeyID;
					cmbKeyName.Text = objTrackTaskBillEntity.CategoryContent_Name;
					if (objTrackTaskBillEntity.TrackTaskBill_AppointStatus == 1)
					{
						ckbAppoint.Checked = true;
					}

					if (objTrackTaskBillEntity.TrackTaskBill_SVHelp == 1)
					{
						ckbSVHelp.Checked = true;
					}

					ClsTrackTaskBill objClsTrackTaskBill = new ClsTrackTaskBill();
					if (BindData(objTrackTaskBillEntity))
					{
						objTrackTaskBillEntity.RackTaskTime_KeyID = Guid.NewGuid().ToString();
						objClsTrackTaskBill.UpdateTrackTaskBill(objTrackTaskBillEntity);
					}

                    this.OpenNewEditForm();
				}
			}
			else
			{
				btnSend.Enabled = false;
				btnDepend.Enabled = false;
				ckbAppoint.Enabled = false;
				cmbMailCategory.Enabled = false;
				cmbKeyName.Enabled = false;
				btnStart.Enabled = false;
				cmbMailCategory.SelectedValue = objTrackTaskBillEntity.CategoryScore_KeyID;
				cmbKeyName.SelectedValue = objTrackTaskBillEntity.CategoryContent_KeyID;
				if (objTrackTaskBillEntity.TrackTaskBill_AppointStatus == 1)
				{
					ckbAppoint.Checked = true;
				}
				ckbMailCheck.Checked = true;


			}
		}

		private void BindData()
		{
			ECCSystem.BizRule.ClsMailCategory objClsMailCategory = new ECCSystem.BizRule.ClsMailCategory();
			DataSet objDS1 = (DataSet)objClsMailCategory.GetList();
			//Don't show blank row

			if (objDS1.Tables.Count > 0)
			{
				DataTable dtTmp = objDS1.Tables[0];
				DataRow dr = dtTmp.NewRow();
				dr[0] = System.DBNull.Value;
				dr[1] = " ";
				dtTmp.Rows.InsertAt(dr, 0);

				cmbMailCategory.ValueMember = "CategoryScore_KeyID";
				cmbMailCategory.DisplayMember = "Category_Name";
				cmbMailCategory.DataSource = dtTmp;

			}
		}

#region ListenMailAgent
        private Thread MailInfoListenThread = null;
	    private IEInstanceResult pIeInstanceResult = null;
	    private delegate void FormDelegate();
        private void GetMailInfo()
        {
            try
            {
                IEInstance _ieInstance = new IEInstance();
                string MailNo = "";
                while (true)
                {

                    this.Invoke(new FormDelegate(delegate()
                                                     {
                                                         this.pIeInstanceResult =
                                                            _ieInstance.GetInfoRes();
                                                     })

                        );

                    if (this.pIeInstanceResult != null
                        && !MailNo.Equals(this.pIeInstanceResult.MailNo))
                    {

                        this.Invoke(new FormDelegate(delegate()
                        {
                            //cmbMailCategory.Text = this.pIeInstanceResult.Category;
                            cmbMailCategory.Text = this.pIeInstanceResult.CategoryFull; 
                            this.TopLevel = true;
                        }));

                        MailNo = this.pIeInstanceResult.MailNo;

                        this.RecordMailLog();
                    }
                    Thread.Sleep(1000);
                }
            }
            //catch (ThreadAbortException ex1)
            //{

            //}
            catch (Exception ex)
            {
                //this.Invoke(new FormDelegate(delegate()
                //                                 {
                //                                     MessageBox.Show(ex.StackTrace);
                //                                    
                //                                 }));
                ExtUtility.logger.Debug("Web Lisener Error");
                ExtUtility.logger.Debug(ex.Message);
                ExtUtility.logger.Debug(ex.StackTrace);
                if(ex.InnerException != null)
                    ExtUtility.logger.Debug(ex.InnerException.Message);

                //throw;
            }
            
        }

        private void RecordMailLog()
        {
            if (this.pIeInstanceResult != null)
            {
                MailLogDao _mailLogDao = new MailLogDao();
                MailLogEntity _mailLogEntity = new MailLogEntity();

                //Record Mail Received
                try
                {
                    if (this.pIeInstanceResult.MailNo != null
                        && !"".Equals(this.pIeInstanceResult.MailNo.Trim(' ')))
                    {
                        _mailLogEntity.MailNo = this.pIeInstanceResult.MailNo;
                        _mailLogEntity.CategoryFullName = this.pIeInstanceResult.CategoryFull;
                        _mailLogEntity.ReceiveDate = DateTime.Parse(this.pIeInstanceResult.RecieveTime);

                        //_mailLogEntity.MailNo = "01C012092900893";
                        //_mailLogEntity.CategoryFullName = "【転送用カテゴリ】大連SV";
                        //_mailLogEntity.ReceiveDate = DateTime.Parse("2012/09/29 18:22:31");

                        if (_mailLogDao.Search(_mailLogEntity).Count == 0)
                            _mailLogDao.SaveOrUpdate(_mailLogEntity);
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show("未找到邮件系统打开的邮件！");
                }
            }
        }
#endregion

	    private void BindForm()
		{
			if (objTrackTaskBillEntity != null)
			{
				if (objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus == 1)
				{
					ckbAppoint.Checked = true;
				}

				//cmbMailCategory.Text = objTrackTaskBillEntity.
			}
		}

		private void cmbMailCategory_SelectedValueChanged(object sender, EventArgs e)
		{
			DateTime objDateTime = new DateTime();
			objDateTime = DateTime.Now;
			bool checkBoxFlg = ckbAppoint.Checked;
			if (cmbMailCategory.Text.Trim() != "")
			{
				ECCSystem.BizRule.ClsMailKeyName objClsMailKeyName = new ECCSystem.BizRule.ClsMailKeyName();
				DataSet objDS1 = (DataSet)objClsMailKeyName.GetListByID(cmbMailCategory.SelectedValue.ToString());
				//Don't show blank row

				if (objDS1.Tables.Count > 0)
				{
					DataTable dt = objDS1.Tables[0];
					DataRow dr = dt.NewRow();
					dr["CategoryContent_KeyID"] = System.DBNull.Value;
					dr["CategoryContent_Name"] = " ";
					dt.Rows.InsertAt(dr, dt.Rows.Count);
					cmbKeyName.ValueMember = "CategoryContent_KeyID";
					cmbKeyName.DisplayMember = "CategoryContent_Name";
					cmbKeyName.DataSource = dt;
				}

				objfrmTemplateImportAndInput.cmbSearch.Text = "件名";
				int intStart = 0;
				int intEnd = 0;
				intStart = cmbMailCategory.Text.IndexOf("【");
				intEnd = cmbMailCategory.Text.IndexOf("】");
				if (intStart >= 0)
				{
					objfrmTemplateImportAndInput.txtSearchKey.Text =
						cmbMailCategory.Text.Substring(intStart, intEnd - intStart + 1);

					objfrmTemplateImportAndInput.btnSearch_Click(null, null);
				}
				else
				{
					objfrmTemplateImportAndInput.btnReflash_Click(null, null);
				}

				objfrmTemplateImportAndInput.txtSearchKey.Text = "";
				objfrmTemplateImportAndInput.cmbSearch.Text = " ";
			}
			else
			{
				cmbKeyName.DataSource = null;
			}
			//if (cmbMailCategory.SelectedValue != null)
			//{
			//    if (cmbMailCategory.SelectedValue.ToString() == "")
			//    {
			//        checkBoxFlg = false;
			//        ckbAppoint.Checked = checkBoxFlg;
			//        return;
			//    }
			//}
			//else
			//{
			//    ckbAppoint.Checked = checkBoxFlg;
			//    return;
			//}


			//ClsEmailAppoint objClsEmailAppoint = new ClsEmailAppoint();
			//DataSet objDataSet = (DataSet)objClsEmailAppoint.GetList();
			//if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
			//{
			//    DataView objMailCategory = objDataSet.Tables[0].DefaultView;
			//    Guid guid = new Guid(cmbMailCategory.SelectedValue.ToString());
			//    objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
			//    objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
			//    if ((objMailCategory.Count > 0) && objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			//    {
			//        checkBoxFlg = true;
			//    }
			//    else
			//    {
			//        checkBoxFlg = false;
			//    }
			//}
			//ckbAppoint.Checked = checkBoxFlg;
		}

		private void checkbox()
		{
			DateTime objDateTime = new DateTime();
			objDateTime = DateTime.Now;
			bool checkBoxFlg = ckbAppoint.Checked;
			if (cmbMailCategory.SelectedValue != null)
			{
				if (cmbMailCategory.SelectedValue.ToString() == "")
				{
					checkBoxFlg = false;
					ckbAppoint.Checked = checkBoxFlg;
					return;
				}
			}
			else
			{
				ckbAppoint.Checked = checkBoxFlg;
				return;
			}


			ClsEmailAppoint objClsEmailAppoint = new ClsEmailAppoint();
			DataSet objDataSet = (DataSet)objClsEmailAppoint.GetList();
			if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
			{
				DataView objMailCategory = objDataSet.Tables[0].DefaultView;
				Guid guid = new Guid(cmbMailCategory.SelectedValue.ToString());
				objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
				objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
				if ((objMailCategory.Count > 0) && objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
				{
					checkBoxFlg = true;
				}
				else
				{
					checkBoxFlg = false;
				}
			}
			ckbAppoint.Checked = checkBoxFlg;

			cmbKeyNameSelectedValueChanged();
		}

		private void btnStart_Click(object sender, EventArgs e)
		{

			if (!ckbAppoint.Checked)
			{
				checkbox();
			}
			dtOperationStart = DateTime.Now;
			ClsTrackTaskBill objClsTrackTaskBill = new ClsTrackTaskBill();
			if (objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			{
				if (!Check())
				{
					return;
				}

				try
				{
					if (objTrackTaskBillEntity == null)
					{
						objTrackTaskBillEntity = new TrackTaskBillEntity();
					}
					objTrackTaskBillEntity.RackTaskTime_KeyID = Guid.NewGuid().ToString();
					if (!BindData(objTrackTaskBillEntity))
					{
						return;
					}

					objClsTrackTaskBill.AddTrackTaskBill(objTrackTaskBillEntity);

				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.ToString(), "ECC Catch Error");
				}
			}
			else
			{
				strMailCheckID = Guid.NewGuid().ToString();
				objClsTrackTaskBill.AddMailCheck(ClsCommon.gIntOperatorID, DateTime.Now, DateTime.Now, strMailCheckID);
			}

			switch (objClsEnumInputMode)
			{
				case ClsEnumInputMode.Enum_TypeStrat:
					btnSend.Enabled = true;
					btnDepend.Enabled = true;
					btnSave.Enabled = true;
					ckbMailCheck.Enabled = false;

					break;
				case ClsEnumInputMode.Enum_TypeSend:
					btnSend.Enabled = true;
					btnDepend.Enabled = true;
					btnSave.Enabled = true;
					ckbMailCheck.Enabled = false;
					ckbAppoint.Enabled = false;
					cmbMailCategory.Enabled = false;
					cmbKeyName.Enabled = false;
					break;
				case ClsEnumInputMode.Enum_TypeSendAgain:
					btnSend.Text = "再転送";
					btnSend.Enabled = true;
					btnDepend.Enabled = true;
					btnSave.Enabled = true;
					ckbMailCheck.Enabled = false;

					break;
				case ClsEnumInputMode.Enum_TypeEnd:
					btnSave.Enabled = true;
					ckbMailCheck.Enabled = false;

					break;
				case ClsEnumInputMode.Enum_TypeDepent:
					btnSend.Enabled = true;
					btnDepend.Enabled = true;
					btnSave.Enabled = true;
					ckbMailCheck.Enabled = false;
					break;
				case ClsEnumInputMode.Enum_TypeMailCheck:
					btnSave.Enabled = true;
					ckbMailCheck.Checked = true;
					ckbMailCheck.Enabled = false;
					ckbAppoint.Enabled = false;
					ckbSVHelp.Enabled = false;
					break;
				default:
					break;
			}
			btnStart.Enabled = false;

			if (objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			{
				ckbMailCheck.Enabled = false;
				ckbAppoint.Enabled = false;
				ckbSVHelp.Enabled = false;
				cmbMailCategory.Enabled = false;
				cmbKeyName.Enabled = false;
				objfrmTemplateImportAndInput.Focus();
			}

            this.OpenNewEditForm();

		}

        private void OpenNewEditForm()
        {
            objfrmTemplateImportAndInput._frmEntryInput = this;
            objfrmTemplateImportAndInput.OpenEditForm();
            objfrmTemplateImportAndInput.TopLevel = false;
        }

		private void cmbKeyName_SelectedValueChanged(object sender, EventArgs e)
		{
			//cmbKeyNameSelectedValueChanged();
		}

		private void cmbKeyNameSelectedValueChanged()
		{
			string strTmpKeyText = "";

			DateTime objDateTime = new DateTime();
			objDateTime = DateTime.Now;
			strKeyNam = cmbKeyName.Text;
			bool checkBoxFlg = ckbAppoint.Checked;
			if (cmbMailCategory.Text.Trim() == "" || cmbKeyName.Text.Trim() == "")
			{
				return;
			}
			int intCountMailCategory = 0;
			ClsEmailAppoint objClsEmailAppoint = new ClsEmailAppoint();
			DataSet objDataSet = (DataSet)objClsEmailAppoint.GetList();
			if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
			{
				DataView objMailCategory = new DataView(objDataSet.Tables[0]);
				DataView objcmbKeyName = new DataView(objDataSet.Tables[0]);
				objMailCategory = objDataSet.Tables[0].DefaultView;

				Guid guid = new Guid(cmbMailCategory.SelectedValue.ToString());
				objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
				objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
				intCountMailCategory = objMailCategory.Count;
				objMailCategory = objDataSet.Tables[0].DefaultView;
				objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
				objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
				objcmbKeyName = objDataSet.Tables[0].DefaultView;

				if (cmbKeyName.Text.Contains(ClsCommon.gstrRe))
				{
					if (strKeyNam.Contains(ClsCommon.gstrRe) && !strKeyNam.Contains(ClsCommon.gstrReLong))
					{
						strTmpKeyText = cmbKeyName.Text;
						cmbKeyName.Text = ClsCommon.gstrRe + ":";
					}
				}

				if (cmbKeyName.SelectedValue != null)
				{
					if (cmbKeyName.SelectedValue.ToString() != "")
					{
						guid = new Guid(cmbKeyName.SelectedValue.ToString());
						objcmbKeyName.RowFilter = "CategoryContent_KeyID = '" + guid + "' AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
						objcmbKeyName.RowFilter = objcmbKeyName.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
					}
					if ((intCountMailCategory > 0 || objcmbKeyName.Count > 0) && objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
					{
						checkBoxFlg = true;
					}
					else
					{
						objMailCategory = objDataSet.Tables[0].DefaultView;
						objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
						objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
						if (objMailCategory.Count <= 0)
						{
							checkBoxFlg = false;
						}
					}
				}

				DataView objTmp = objDataSet.Tables[0].DefaultView;
				objTmp.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
				objTmp.RowFilter = objTmp.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
				if (objTmp.Count <= 0)
				{
					if (cmbKeyName.Text.Contains(ClsCommon.gstrRe))
					{
						if (strKeyNam.Contains(ClsCommon.gstrRe) && !strKeyNam.Contains(ClsCommon.gstrReLong))
						{
							ckbAppoint.Checked = checkBoxFlg;
							cmbKeyName.Text = strTmpKeyText;
							return;
						}
					}

					//if (strKeyNam.Trim() != cmbKeyName.Text.Trim())
					//{
					//    checkBoxFlg = false;
					//}
					//else
					//{
					//    checkBoxFlg = true;
					//}
				}
			}

			ckbAppoint.Checked = checkBoxFlg;
		}

		private void cmbKeyName_TextChanged(object sender, EventArgs e)
		{
			//DateTime objDateTime = new DateTime();
			//objDateTime = DateTime.Now;
			//bool checkBoxFlg = ckbAppoint.Checked;
			//if (cmbMailCategory.Text.Trim() == "" || cmbKeyName.Text.Trim() == "")
			//{
			//    return;
			//}
			//int intCountMailCategory = 0;
			//ClsEmailAppoint objClsEmailAppoint = new ClsEmailAppoint();
			//DataSet objDataSet = (DataSet)objClsEmailAppoint.GetList();
			//DataView objMailCategory = new DataView(objDataSet.Tables[0]);
			//DataView objcmbKeyName = new DataView(objDataSet.Tables[0]);
			//if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
			//{
			//    objMailCategory = objDataSet.Tables[0].DefaultView;
			//    objcmbKeyName = objDataSet.Tables[0].DefaultView;
			//    Guid guid = new Guid(cmbMailCategory.SelectedValue.ToString());
			//    objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
			//    objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
			//    intCountMailCategory = objMailCategory.Count;
			//    if (cmbKeyName.SelectedValue != null)
			//    {
			//        if (cmbKeyName.SelectedValue.ToString() != "")
			//        {
			//            guid = new Guid(cmbKeyName.SelectedValue.ToString());
			//            objcmbKeyName.RowFilter = "CategoryContent_KeyID = '" + guid + "' AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
			//            objcmbKeyName.RowFilter = objcmbKeyName.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
			//        }
			//        if ((intCountMailCategory > 0 || objcmbKeyName.Count > 0) && objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			//        {
			//            checkBoxFlg = true;
			//        }
			//        else
			//        {
			//            objMailCategory = objDataSet.Tables[0].DefaultView;
			//            objMailCategory.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
			//            objMailCategory.RowFilter = objMailCategory.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
			//            if (objMailCategory.Count <= 0)
			//            {
			//                checkBoxFlg = false;
			//            }
			//        }
			//    }
			//    DataView objTmp = objDataSet.Tables[0].DefaultView;
			//    objTmp.RowFilter = "CategoryScore_KeyID = '" + guid + "' AND CategoryContent_KeyID is null AND EmailAppoint_Member like '%" + ClsCommon.gStrOperatorName + "|%'";
			//    objTmp.RowFilter = objTmp.RowFilter + " And EmailAppoint_StarTime <= '" + objDateTime + "' and EmailAppoint_EndTime >= '" + objDateTime + "'";
			//    if (objTmp.Count <= 0)
			//    {
			//        if (cmbKeyName.Text.Contains(ClsCommon.gstrRe))
			//        {
			//            if (strKeyNam.Contains(ClsCommon.gstrRe) && !strKeyNam.Contains(ClsCommon.gstrReLong))
			//            {
			//                ckbAppoint.Checked = checkBoxFlg;
			//                return;
			//            }
			//        }

			//        if (strKeyNam != cmbKeyName.Text)
			//        {
			//            checkBoxFlg = false;
			//        }
			//        else
			//        {
			//            checkBoxFlg = true;
			//        }
			//    }
			//}
			//ckbAppoint.Checked = checkBoxFlg;
		}

        public void EntryInputAction(Keys key)
	    {
            switch (key)
            {
                case Keys.Enter:
                    this.btnSave_Click(null, null);
                    return;
                case Keys.Back:
                    this.btnSend_Click(null, null);
                    return;
            }
        }

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			{
				objClsEnumInputMode = ClsEnumInputMode.Enum_TypeEnd;
			}
			if (!BindData(objTrackTaskBillEntity))
			{
				return;
			}
			SubData(objTrackTaskBillEntity);
			
            objfrmTemplateImportAndInput.SendToMCS();
		}

		private void btnDepend_Click(object sender, EventArgs e)
		{
			objClsEnumInputMode = ClsEnumInputMode.Enum_TypeDepent;
			if (!BindData(objTrackTaskBillEntity))
			{
				return;
			}
			SubData(objTrackTaskBillEntity);
            objfrmTemplateImportAndInput.SendToMCS();
		}

		private void btnSend_Click(object sender, EventArgs e)
		{
			if (objClsEnumInputMode == ClsEnumInputMode.Enum_TypeStrat)
			{
				objClsEnumInputMode = ClsEnumInputMode.Enum_TypeSend;
			}
			else
			{
			
				//objClsEnumInputMode = ClsEnumInputMode.Enum_TypeSendAgain;
			}
			if (!BindData(objTrackTaskBillEntity))
			{
				return;
			}
			SubData(objTrackTaskBillEntity);
            objfrmTemplateImportAndInput.SendToMCS();
		}

		private bool Check()
		{
			if (cmbMailCategory.Text.ToString().Trim().Length == 0)
			{
				MessageBox.Show("カテゴリを入力してください！", "ECC Information");
				cmbMailCategory.Focus();
				return false;
			}

			if (cmbKeyName.Text.Trim() == "")
			{
				MessageBox.Show("件名を入力してください！", "ECC Information");
				cmbKeyName.Focus();
				return false;
			}

			return true;
		}

		private bool BindData(TrackTaskBillEntity objTrackTaskBillEntity)
		{
			if (objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
			{
				if (!Check())
				{
					return false;
				}
			}

			DateTime objTime = new DateTime();
			objTime = DateTime.Now;
			TimeSpan objTimeSpan = objTime.Subtract(dtOperationStart);
			fltOperationTime = Convert.ToInt64(objTimeSpan.TotalSeconds);

			ClsOverTimeBill objClsOverTimeBill = new ClsOverTimeBill();

			DataSet objDataSet = (DataSet)objClsOverTimeBill.GetList(dtOperationStart);

			if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
			{
				DataTable objDataTable = objDataSet.Tables[0];
				for (int i = 0; i < objDataTable.Rows.Count; i++)
				{
					string[] strArr = objDataTable.Rows[0]["OvertimeBill_PersonID"].ToString().Split(new char[] {Convert.ToChar("|")});

					foreach (string var in strArr)
					{
						if (var.Trim() == ClsCommon.gIntOperatorID.ToString())
						{
							intOT = 1;
							strOTKeyID = objDataTable.Rows[0]["OvertimeBill_KeyID"].ToString();
						}
					}
				}
			}
			if (objTrackTaskBillEntity == null)
			{
				objTrackTaskBillEntity = new TrackTaskBillEntity();
			}
			objTrackTaskBillEntity.TrackTaskBill_EndTime = objTime;
			
			if (objTypeTemp == TypeTemp.Enum_TypeTempAdd)
			{
				objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = intOT;
				objTrackTaskBillEntity.OvertimeBill_KeyID = strOTKeyID;
				objTrackTaskBillEntity.TrackTaskBill_UserID = ClsCommon.gIntOperatorID;
				objTrackTaskBillEntity.TrackTaskBill_OperatePerson = ClsCommon.gStrOperatorName;

				objTrackTaskBillEntity.CategoryScore_KeyID = clsUICheck.NullToString(cmbMailCategory.SelectedValue);
				objTrackTaskBillEntity.Category_Name = cmbMailCategory.Text.Trim();
				objTrackTaskBillEntity.CategoryContent_Name = cmbKeyName.Text.Trim();
				objTrackTaskBillEntity.CategoryContent_KeyID = clsUICheck.NullToString(cmbKeyName.SelectedValue);
			}

			objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
			if (ckbAppoint.Checked)
			{
				objTrackTaskBillEntity.TrackTaskBill_AppointStatus = 1;
			}
			else
			{
				objTrackTaskBillEntity.TrackTaskBill_AppointStatus = 0;
			}
			if (ckbSVHelp.Checked)
			{
				objTrackTaskBillEntity.TrackTaskBill_SVHelp = 1;
			}
			else
			{
				objTrackTaskBillEntity.TrackTaskBill_SVHelp = 0;
			}

			switch (objClsEnumInputMode)
			{
				case ClsEnumInputMode.Enum_TypeSend:

					objTrackTaskBillEntity.TrackTaskBill_Status = (int)objClsEnumInputMode;
					objTrackTaskBillEntity.TrackTaskBill_SendNumber = 1;
					objTrackTaskBillEntity.TrackTaskBill_SendTime = objTime;
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					objTrackTaskBillEntity.TrackTaskBill_SendTimeSpan = fltOperationTime + objTrackTaskBillEntity.TrackTaskBill_SendTimeSpan;
					objTrackTaskBillEntity.TrackTaskBill_EndTime = objTime;
					objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = intOT;
					objTrackTaskBillEntity.OvertimeBill_KeyID = strOTKeyID;
					//objClsTrackTaskBill.AddTrackTaskBill(objTrackTaskBillEntity);
					break;
				case ClsEnumInputMode.Enum_TypeSendAgain:
					objTrackTaskBillEntity.TrackTaskBill_SendNumberAgain = objTrackTaskBillEntity.TrackTaskBill_SendNumberAgain;
					objTrackTaskBillEntity.TrackTaskBill_SendTimeAgain = objTime;
					objTrackTaskBillEntity.TrackTaskBill_Status = (int)objClsEnumInputMode;
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					objTrackTaskBillEntity.TrackTaskBill_SendTimeAgainSpan = fltOperationTime + objTrackTaskBillEntity.TrackTaskBill_SendTimeSpan;
					//objClsTrackTaskBill.UpdateTrackTaskBill(objTrackTaskBillEntity);
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					objTrackTaskBillEntity.TrackTaskBill_EndTime = objTime;
					objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = intOT;
					objTrackTaskBillEntity.OvertimeBill_KeyID = strOTKeyID;
					break;
				case ClsEnumInputMode.Enum_TypeEnd:
					//objTrackTaskBillEntity.TrackTaskBill_SendNumberAgain = 1 + objTrackTaskBillEntity.TrackTaskBill_SendNumberAgain;
					objTrackTaskBillEntity.TrackTaskBill_SendTimeAgain = objTime;
					objTrackTaskBillEntity.TrackTaskBill_Status = (int)objClsEnumInputMode;
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					objTrackTaskBillEntity.TrackTaskBill_EndTimeSpan = fltOperationTime + objTrackTaskBillEntity.TrackTaskBill_SendTimeSpan;
					//objClsTrackTaskBill.UpdateTrackTaskBill(objTrackTaskBillEntity);
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					objTrackTaskBillEntity.TrackTaskBill_EndTime = objTime;
					objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = intOT;
					objTrackTaskBillEntity.OvertimeBill_KeyID = strOTKeyID;
					break;
				case ClsEnumInputMode.Enum_TypeDepent:
					//objTrackTaskBillEntity.CategoryContent_KeyID = cmbKeyName.SelectedValue.ToString();
					objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = intOT;
					objTrackTaskBillEntity.OvertimeBill_KeyID = strOTKeyID;
					objTrackTaskBillEntity.TrackTaskBill_Status = (int)objClsEnumInputMode;
					objTrackTaskBillEntity.TrackTaskBill_UserID = ClsCommon.gIntOperatorID;
					objTrackTaskBillEntity.TrackTaskBill_OperatePerson = ClsCommon.gStrOperatorName;
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					//objTrackTaskBillEntity.TrackTaskBill_StartDate = dtOperationStart;
					//objTrackTaskBillEntity.TrackTaskBill_SendTimeAgainSpan = fltOperationTime + objTrackTaskBillEntity.TrackTaskBill_SendTimeSpan;
					//objClsTrackTaskBill.UpdateTrackTaskBill(objTrackTaskBillEntity);
					objTrackTaskBillEntity.TrackTaskBill_EndTime = objTime;
					objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = intOT;
					objTrackTaskBillEntity.OvertimeBill_KeyID = strOTKeyID;
					break;
				case ClsEnumInputMode.Enum_TypeMailCheck:

					
					break;
				default:
					break;
			}

			return true;
		}

		private void SubData(TrackTaskBillEntity objTrackTaskBillEntity)
		{
			DateTime objTime = new DateTime();
			objTime = DateTime.Now;
			TimeSpan objTimeSpan = objTime.Subtract(dtOperationStart);
			fltOperationTime = Convert.ToInt64(objTimeSpan.TotalSeconds);

			try
			{
				ClsTrackTaskBill objClsTrackTaskBill = new ClsTrackTaskBill();
				switch (objClsEnumInputMode)
				{
					case ClsEnumInputMode.Enum_TypeSend:

						break;
					case ClsEnumInputMode.Enum_TypeSendAgain:
	
						break;
					case ClsEnumInputMode.Enum_TypeEnd:

						break;
					case ClsEnumInputMode.Enum_TypeDepent:

						break;
					case ClsEnumInputMode.Enum_TypeMailCheck:

						objClsTrackTaskBill.UpdateMailCheck(ClsCommon.gIntOperatorID, dtOperationStart, DateTime.Now, strMailCheckID);
						break;
					default:
						break;
				}

				if (objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
				{
					objClsTrackTaskBill.SubmitTrackTaskBill(objTrackTaskBillEntity);
				}
				objTrackTaskBillEntity = null;
				//if (objTypeTemp == TypeTemp.Enum_TypeTempAdd && objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
				//{
				//    objClsTrackTaskBill.AddTrackTaskBill(objTrackTaskBillEntity);
				//    objTrackTaskBillEntity = null;
				//}
				//else if (objTypeTemp == TypeTemp.Enum_TypeTempUpdate && objClsEnumInputMode != ClsEnumInputMode.Enum_TypeMailCheck)
				//{
				//    objClsTrackTaskBill.UpdateTrackTaskBill(objTrackTaskBillEntity);
				//}

				if (objTypeTemp == TypeTemp.Enum_TypeTempAdd)
				{
					if (ckbMailCheck.Checked == true)
					{
						btnStart.Enabled = true;
						btnSend.Enabled = false;
						btnDepend.Enabled = false;
						btnSave.Enabled = false;
						cmbMailCategory.Enabled = false;
						cmbKeyName.Enabled = false;
						ckbAppoint.Enabled = false;
						ckbSVHelp.Enabled = false;
						ckbMailCheck.Enabled = true;
					}
					else
					{
						objTrackTaskBillEntity = null;
						btnSend.Enabled = false;
						btnStart.Enabled = true;
						btnDepend.Enabled = false;
						btnSave.Enabled = false;
						ckbAppoint.Enabled = true;
						ckbSVHelp.Enabled = true;
						ckbMailCheck.Enabled = false;
						ckbSVHelp.Checked = false;
						cmbMailCategory.Enabled = true;
						cmbMailCategory.Text = " ";
						cmbKeyName.Enabled = true;
						cmbKeyName.Text = " ";
						objClsEnumInputMode = ClsEnumInputMode.Enum_TypeStrat;
					}

				}
				else
				{
					btnStart.Enabled = true;
					this.Close();
				}
				ckbAppoint.Checked = false;
				ckbMailCheck.Checked = false;
				ckbSVHelp.Checked = false;
			}
			catch(Exception ex)
			{
				objTrackTaskBillEntity = null;
				MessageBox.Show(ex.ToString(), "ECC Catch Error");
			}
		}
		private void cmbMailCategory_Validated(object sender, EventArgs e)
		{
			//string strTmp = cmbMailCategory.Text;
			//DataTable objDataTable = new DataTable();
			//objDataTable = (DataTable)cmbMailCategory.DataSource;
			//DataView objDataView = objDataTable.DefaultView;
			//objDataView.RowFilter = cmbMailCategory.DisplayMember + " = '" + cmbMailCategory.Text.Trim() + "'";
			//if (objDataView.Count == 0)
			//{
			//    objDataView.RowFilter = "";
			//    cmbMailCategory.Text = strTmp;
			//    return;
			//}
			//else
			//{
			//    string obj = "";
			//    obj = objDataView[0][cmbMailCategory.ValueMember].ToString();
			//    objDataView.RowFilter = "";
			//    cmbMailCategory.SelectedValue = obj;
			//    cmbMailCategory_SelectedValueChanged(null, null);
			//}
			
		}

		private void cmbKeyName_Validated(object sender, EventArgs e)
		{
			//string strTmp = cmbKeyName.Text;
			//DataTable objDataTable = new DataTable();
			//objDataTable = (DataTable)cmbKeyName.DataSource;
			//if (objDataTable == null)
			//{
			//    return;
			//}
			//DataView objDataView = objDataTable.DefaultView;
			//objDataView.RowFilter = cmbKeyName.DisplayMember + " = '" + cmbKeyName.Text.Trim() + "'";
			//if (objDataView.Count == 0)
			//{
			//    objDataView.RowFilter = "";
			//    cmbKeyName.SelectedValue = System.DBNull.Value;
			//    cmbKeyName.Text = strTmp;
			//    return;
			//}
			//else
			//{
			//    string obj = "";
			//    obj = objDataView[0][cmbKeyName.ValueMember].ToString();
			//    objDataView.RowFilter = "";
			//    cmbKeyName.SelectedValue = obj;
			//    cmbKeyNameSelectedValueChanged();
			//}
		}

		private void ckbMailCheck_Click(object sender, EventArgs e)
		{

		}

		private void ckbMailCheck_CheckedChanged(object sender, EventArgs e)
		{
			if (ckbMailCheck.Checked == false)
			{
				objClsEnumInputMode = objClsEnumInputModeTmp;
				btnStart.Enabled = true;
				btnSend.Enabled = false;
				btnDepend.Enabled = false;
				btnSave.Enabled = false;
				cmbMailCategory.Enabled = true;
				cmbKeyName.Enabled = true;
				ckbAppoint.Enabled = true;
				ckbSVHelp.Enabled = true;

			}
			else
			{
				objClsEnumInputModeTmp = objClsEnumInputMode;
				objClsEnumInputMode = ClsEnumInputMode.Enum_TypeMailCheck;
				btnStart.Enabled = true;
				btnSend.Enabled = false;
				btnDepend.Enabled = false;
				btnSave.Enabled = false;
				cmbMailCategory.Enabled = false;
				cmbKeyName.Enabled = false;
				ckbAppoint.Enabled = false;
				ckbSVHelp.Enabled = false;

			}
		}

		private void frmEntryInput_FormClosing(object sender, FormClosingEventArgs e)
		{
			//if (btnStart.Enabled == false && objTypeTemp == TypeTemp.Enum_TypeTempAdd)
			if (btnStart.Enabled == false)
			{
				MessageBox.Show("画面上のデータは保存されていません、保存してからログアウトして下さい。", "ECC Information");
				e.Cancel = true;
			}
			else
			{
				Form objfrmEntrySearch = new frmEntrySearch();
				objfrmEntrySearch.Show();
				objfrmTemplateImportAndInput.Close();
			}



		}

		private void ckbSVHelp_CheckedChanged(object sender, EventArgs e)
		{

			if (!ckbAppoint.Checked && !ckbSVHelp.Checked)
			{
				ckbMailCheck.Enabled = true;
			}
			else
			{
				ckbMailCheck.Enabled = false;
			}
			if (ckbSVHelp.Checked)
			{
				ckbAppoint.Checked = !ckbSVHelp.Checked;
			}
			
		}

		private void ckbAppoint_CheckedChanged(object sender, EventArgs e)
		{
			if (!ckbAppoint.Checked && !ckbSVHelp.Checked)
			{
				ckbMailCheck.Enabled = true;
			}
			else
			{
				ckbMailCheck.Enabled = false;
			}

			if (ckbAppoint.Checked)
			{
				ckbSVHelp.Checked = !ckbAppoint.Checked;
			}
		}

        private void frmEntryInput_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Invoke(new FormDelegate(delegate()
            //{
            //    if (MailInfoListenThread != null)
            //        MailInfoListenThread.Abort();

            //    while (MailInfoListenThread.IsAlive)
            //    {
            //        Thread.Sleep(200);
            //    }
            //}));

            if (MailInfoListenThread != null)
                MailInfoListenThread.Abort();
        }

	}
}